package com.example.experiment4;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import java.io.File;

public class NotificationActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notification);
        NotificationManager manager = (NotificationManager)
                getSystemService(NOTIFICATION_SERVICE);
        manager.cancel(1);
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    public void clickNotification(View source) {
        Intent intent = new Intent(this, SMSActivity.class);
        PendingIntent pi = PendingIntent.getActivity(this, 0, intent,
                PendingIntent.FLAG_CANCEL_CURRENT);

        NotificationManager manager = (NotificationManager)
                getSystemService (NOTIFICATION_SERVICE);

        Notification notification;
        Notification.Builder builder;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            builder=new Notification.Builder(this,"5996773");
        }else {
            builder=new Notification.Builder(this);
        }
        //设置标题
        builder.setContentTitle("新的测试题目");
        //设置内容
        builder.setContentText("Android应用界面中UI（User Interface）组件间有严格的层次关系，有哪两种类型的视图组件？");
        //设置状态栏显示的图标
        builder.setSmallIcon(R.mipmap.ic_launcher);
        // 设置通知灯光（LIGHTS）、铃声（SOUND）、震动（VIBRATE）、（ALL 表示都设置）
        builder.setDefaults(Notification.DEFAULT_ALL);
        //灯光三个参数，颜色（argb）、亮时间（毫秒）、暗时间（毫秒）,灯光与设备有关
        builder.setLights(Color.RED, 200, 200);
        // 铃声,传入铃声的 Uri（可以本地或网上）我这没有铃声就不传了
        builder.setSound(Uri.parse("")) ;
        // 震动，传入一个 long 型数组，表示 停、震、停、震 ... （毫秒）
        builder.setVibrate(new long[]{0, 200, 200, 200, 200, 200});
        // 通知栏点击后自动消失
        builder.setAutoCancel(true);
        // 简单通知栏设置 Intent
        builder.setContentIntent(pi);
        builder.setPriority(Notification.PRIORITY_HIGH);

        //设置下拉之后显示的图片
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),
                R.drawable.ic_intent));

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel("5996773",
                    "安卓10a", NotificationManager.IMPORTANCE_DEFAULT);
            channel.enableLights(true);           // 是否在桌面icon右上角展示小红点
            channel.setLightColor(Color.GREEN);   // 小红点颜色
            channel.setShowBadge(false);          // 是否在久按桌面图标时显示此渠道的通知
            manager.createNotificationChannel(channel);
        }

        notification = builder.build();
        manager.notify(1,notification);
    }
}
